home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / mint / utilit~1 / futilsrc.zoo / fileutil / src / df.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-08-26  |  9.0 KB  |  395 lines

  1. /* df - summarize free disk space
  2.    Copyright (C) 1991 Free Software Foundation, Inc.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 2, or (at your option)
  7.    any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. /* Usage: df [-aikP] [-t fstype] [+all] [+inodes] [+type fstype]
  19.    [+kilobytes] [+portability] [path...]
  20.  
  21.    Options:
  22.    -a, +all        List all filesystems, even zero-size ones.
  23.    -i, +inodes        List inode usage information instead of block usage.
  24.    -k, +kilobytes    Print sizes in 1K blocks instead of 512-byte blocks.
  25.    -P, +portability    Use the POSIX output format (one line per filesystem).
  26.    -t, +type fstype    Limit the listing to filesystems of type `fstype'.
  27.             Multiple -t options can be given.
  28.             By default, all filesystem types are listed.
  29.  
  30.    Written by David MacKenzie <djm@ai.mit.edu> */
  31.  
  32. #include <stdio.h>
  33. #include <sys/types.h>
  34. #include <getopt.h>
  35. #include "fsinfo.h"
  36. #include "system.h"
  37.  
  38. void sync ();
  39.  
  40. char *strstr ();
  41. char *xmalloc ();
  42. char *xstrdup ();
  43. int fs_to_list ();
  44. int get_fs_usage ();
  45. struct mount_entry *read_filesystem_list ();
  46. void add_fs_type ();
  47. void error ();
  48. void print_header ();
  49. void show_entry ();
  50. void show_all_entries ();
  51. void show_dev ();
  52. void show_disk ();
  53. void show_point ();
  54. void usage ();
  55.  
  56. /* If nonzero, show inode information. */
  57. int inode_format;
  58.  
  59. /* If nonzero, show even filesystems with zero size. */
  60. int show_empty_fs;
  61.  
  62. /* If nonzero, use 1K blocks instead of 512-byte blocks. */
  63. int kilobyte_blocks;
  64.  
  65. /* If nonzero, use the POSIX output format.  */
  66. int posix_format;
  67.  
  68. /* Nonzero if errors have occurred. */
  69. int exit_status;
  70.  
  71. /* Name this program was run with. */
  72. char *program_name;
  73.  
  74. /* A filesystem type to display. */
  75.  
  76. struct fs_select
  77. {
  78.   char *fs_name;
  79.   struct fs_select *fs_next;
  80. };
  81.  
  82. /* Linked list of filesystem types to display.
  83.    If `fs_list' is NULL, list all types.
  84.    This table is generated dynamically from command-line options,
  85.    rather than hardcoding into the program what it thinks are the
  86.    valid filesystem types; let the user specify any filesystem type
  87.    they want to, and if there are any filesystems of that type, they
  88.    will be shown.
  89.  
  90.    Some filesystem types:
  91.    4.2 4.3 ufs nfs swap ignore io vm */
  92.  
  93. struct fs_select *fs_list;
  94.  
  95. /* Linked list of mounted filesystems. */
  96. struct mount_entry *mount_list;
  97.  
  98. struct option long_options[] =
  99. {
  100.   {"all", 0, &show_empty_fs, 1},
  101.   {"inodes", 0, &inode_format, 1},
  102.   {"kilobytes", 0, &kilobyte_blocks, 1},
  103.   {"portability", 0, &posix_format, 1},
  104.   {"type", 1, 0, 't'},
  105.   {NULL, 0, NULL, 0}
  106. };
  107.  
  108. void
  109. main (argc, argv)
  110.      int argc;
  111.      char **argv;
  112. {
  113.   int optc;
  114.  
  115.   program_name = argv[0];
  116.   fs_list = NULL;
  117.   inode_format = 0;
  118.   show_empty_fs = 0;
  119.   kilobyte_blocks = getenv ("POSIX_ME_HARDER") == 0;
  120.   posix_format = 0;
  121.   exit_status = 0;
  122.  
  123.   while ((optc = getopt_long (argc, argv, "aikPt:", long_options, (int *) 0))
  124.      != EOF)
  125.     {
  126.       switch (optc)
  127.     {
  128.     case 0:            /* Long option. */
  129.       break;
  130.     case 'a':
  131.       show_empty_fs = 1;
  132.       break;
  133.     case 'i':
  134.       inode_format = 1;
  135.       break;
  136.     case 'k':
  137.       kilobyte_blocks = 1;
  138.       break;
  139.     case 'P':
  140.       posix_format = 1;
  141.       break;
  142.     case 't':
  143.       add_fs_type (optarg);
  144.       break;
  145.     default:
  146.       usage ();
  147.     }
  148.     }
  149.  
  150.   mount_list = read_filesystem_list (fs_list != NULL);
  151.   if (mount_list == NULL)
  152.     error (1, errno, "cannot read table of mounted filesystems");
  153.  
  154.   print_header ();
  155.  
  156.   if (optind == argc)
  157.     show_all_entries ();
  158.   else
  159.     {
  160.       /* Display explicitly requested empty filesystems. */
  161.       show_empty_fs = 1;
  162.       for (; optind < argc; ++optind)
  163.     show_entry (argv[optind]);
  164.     }
  165.  
  166.   exit (exit_status);
  167. }
  168.  
  169. void
  170. print_header ()
  171. {
  172.   if (inode_format)
  173.     printf ("Filesystem             iused   ifree  %%iused");
  174.   else
  175.     printf ("Filesystem         %s  used available capacity",
  176.         kilobyte_blocks ? "1024-blocks" : " 512-blocks");
  177.   printf (" Mounted on\n");
  178. }
  179.  
  180. /* Determine what kind of node PATH is and show the disk usage
  181.    for it. */
  182.  
  183. void
  184. show_entry (path)
  185.      char *path;
  186. {
  187.   struct stat path_stats;
  188.  
  189.   if (stat (path, &path_stats))
  190.     {
  191.       error (0, errno, "%s", path);
  192.       exit_status = 1;
  193.       return;
  194.     }
  195.   if (S_ISBLK (path_stats.st_mode) || S_ISCHR (path_stats.st_mode))
  196.     show_disk (path);
  197.   else
  198.     show_point (path);
  199. }
  200.  
  201. /* Identify the directory, if any, that device
  202.    DISK is mounted on, and show its disk usage.  */
  203.  
  204. void
  205. show_disk (disk)
  206.      char *disk;
  207. {
  208.   struct mount_entry *me;
  209.  
  210.   for (me = mount_list; me; me = me->me_next)
  211.     if (!strcmp (disk, me->me_devname))
  212.       {
  213.     show_dev (me->me_devname, me->me_mountdir, me->me_type);
  214.     return;
  215.       }
  216.   /* No filesystem is mounted on DISK. */
  217.   show_dev (disk, (char *) NULL, (char *) NULL);
  218. }
  219.  
  220. /* Identify the device that file or directory POINT
  221.    is mounted on, and show its disk usage.  */
  222.  
  223. void
  224. show_point (point)
  225.      char *point;
  226. {
  227.   struct stat point_stats;
  228.   struct stat disk_stats;
  229.   struct mount_entry *me;
  230.  
  231.   if (stat (point, &point_stats))
  232.     {
  233.       error (0, errno, "%s", point);
  234.       exit_status = 1;
  235.       return;
  236.     }
  237.  
  238.   for (me = mount_list; me; me = me->me_next)
  239.     {
  240.       if (me->me_dev == -1)
  241.     {
  242.       if (stat (me->me_mountdir, &disk_stats) == 0)
  243.         me->me_dev = disk_stats.st_dev;
  244.       else
  245.         {
  246.           error (0, errno, "%s", me->me_mountdir);
  247.           exit_status = 1;
  248.           me->me_dev = -2;    /* So we won't try and fail repeatedly. */
  249.         }
  250.     }
  251.       if (point_stats.st_dev == me->me_dev)
  252.     {
  253.       show_dev (me->me_devname, me->me_mountdir, me->me_type);
  254.       return;
  255.     }
  256.     }
  257.   error (0, 0, "cannot find mount point for %s", point);
  258.   exit_status = 1;
  259. }
  260.  
  261. /* Display a space listing for the disk device with absolute path DISK.
  262.    If MOUNT_POINT is non-NULL, it is the path of the root of the
  263.    filesystem on DISK.
  264.    If FSTYPE is non-NULL, it is the type of the filesystem on DISK. */
  265.  
  266. void
  267. show_dev (disk, mount_point, fstype)
  268.      char *disk;
  269.      char *mount_point;
  270.      char *fstype;
  271. {
  272.   struct fs_usage fsu;
  273.   long blocks_used;
  274.   long blocks_percent_used;
  275.   long inodes_used;
  276.   long inodes_percent_used;
  277.   char *stat_file;
  278.  
  279.   if (!fs_to_list (fstype))
  280.     return;
  281.  
  282.   /* If MOUNT_POINT is NULL, then the filesystem is not mounted, and this
  283.      program reports on the filesystem that the special file is on.
  284.      It would be better to somehow report on the unmounted filesystem,
  285.      but statfs doesn't work on those on many systems. */
  286.   stat_file = mount_point ? mount_point : disk;
  287.  
  288.   sync ();
  289.   if (get_fs_usage (stat_file, &fsu))
  290.     {
  291.       error (0, errno, "%s", stat_file);
  292.       exit_status = 1;
  293.       return;
  294.     }
  295.  
  296.   if (!kilobyte_blocks)
  297.     {
  298.       fsu.fsu_blocks *= 2;
  299.       fsu.fsu_bfree *= 2;
  300.       fsu.fsu_bavail *= 2;
  301.     }
  302.  
  303.   if (fsu.fsu_blocks == 0)
  304.     {
  305.       if (show_empty_fs == 0)
  306.     return;
  307.       blocks_used = fsu.fsu_bavail = blocks_percent_used = 0;
  308.     }
  309.   else
  310.     {
  311.       blocks_used = fsu.fsu_blocks - fsu.fsu_bfree;
  312.       blocks_percent_used = (long)
  313.     (blocks_used * 100.0 / (blocks_used + fsu.fsu_bavail) + 0.5);
  314.     }
  315.  
  316.   if (fsu.fsu_files == 0)
  317.     {
  318.       inodes_used = fsu.fsu_ffree = inodes_percent_used = 0;
  319.     }
  320.   else
  321.     {
  322.       inodes_used = fsu.fsu_files - fsu.fsu_ffree;
  323.       inodes_percent_used = (long)
  324.     (inodes_used * 100.0 / fsu.fsu_files + 0.5);
  325.     }
  326.  
  327.   printf ("%-20s", disk);
  328.   if (strlen (disk) > 20 && !posix_format)
  329.     printf ("\n                    ");
  330.  
  331.   if (inode_format)
  332.     printf (" %7ld %7ld %5ld%%",
  333.         inodes_used, fsu.fsu_ffree, inodes_percent_used);
  334.   else
  335.     printf (" %7ld %7ld  %7ld  %5ld%% ",
  336.         fsu.fsu_blocks, blocks_used, fsu.fsu_bavail, blocks_percent_used);
  337.  
  338.   if (mount_point)
  339.     printf ("  %s", mount_point);
  340.   putchar ('\n');
  341. }
  342.  
  343. /* Show all mounted filesystems, except perhaps those that are of
  344.    an unselected type or are empty. */
  345.  
  346. void
  347. show_all_entries ()
  348. {
  349.   struct mount_entry *me;
  350.  
  351.   for (me = mount_list; me; me = me->me_next)
  352.     show_dev (me->me_devname, me->me_mountdir, me->me_type);
  353. }
  354.  
  355. /* Add FSTYPE to the list of filesystem types to display. */
  356.  
  357. void
  358. add_fs_type (fstype)
  359.      char *fstype;
  360. {
  361.   struct fs_select *fsp;
  362.  
  363.   fsp = (struct fs_select *) xmalloc (sizeof (struct fs_select));
  364.   fsp->fs_name = fstype;
  365.   fsp->fs_next = fs_list;
  366.   fs_list = fsp;
  367. }
  368.  
  369. /* If FSTYPE is a type of filesystem that should be listed,
  370.    return nonzero, else zero. */
  371.  
  372. int
  373. fs_to_list (fstype)
  374.      char *fstype;
  375. {
  376.   struct fs_select *fsp;
  377.  
  378.   if (fs_list == NULL || fstype == NULL)
  379.     return 1;
  380.   for (fsp = fs_list; fsp; fsp = fsp->fs_next)
  381.     if (!strcmp (fstype, fsp->fs_name))
  382.       return 1;
  383.   return 0;
  384. }
  385.  
  386. void
  387. usage ()
  388. {
  389.   fprintf (stderr, "\
  390. Usage: %s [-aikP] [-t fstype] [+all] [+inodes] [+type fstype]\n\
  391.        [+kilobytes] [+portability] [path...]\n",
  392.        program_name);
  393.   exit (1);
  394. }
  395.